<?php

require 'lib/model/om/BaseBlocPeer.php';

class BlocPeer extends BaseBlocPeer
{
    /**
     * Se adauga conditia ca user_id-ul sa fie cel al user-ului logat
     *
     * @param type $pk
     * @param PropelPDO $con
     * @return type
     */
    static public function retrieveByPK($pk, PropelPDO $con = null)
    {
        $object = parent::retrieveByPK($pk, $con);

        return $object && $object->getUserId() == sfContext::getInstance()->getUser()->getId() ? $object : null;
    }

    /**
     * Returneaza un array cu elemente de forma:
     * index => array(ID, NAME, INCOMPLETE)
     *
     * @return array
     */
    static public function getBlocuri()
    {
        $criteria = new UserCriteria(__CLASS__);
        $columns = array(self::ID, self::NAME);

        $blocuri = Peer::fetchColumns(get_class(), $criteria, $columns);
        foreach ($blocuri as $index => $bloc)
        {
            $blocuri[$index]['INCOMPLETE'] = BlocPeer::retrieveByPK($bloc['ID'])->isIncomplete();
        }

        return $blocuri;
    }

    static public function getScara($bloc_id)
    {
        $criteria = new UserCriteria('ScaraPeer');
        $criteria->add(ScaraPeer::BLOC_ID, $bloc_id);
        $columns = array(ScaraPeer::ID, ScaraPeer::NAME);

        $scara = Peer::fetchColumns('ScaraPeer', $criteria, $columns);
        foreach ($scara as $index => $value)
        {
            $scara[$index]['INCOMPLETE'] = ScaraPeer::retrieveByPK($value['ID'])->isIncomplete();
        }

        return $scara;
    }

    static public function getBlocuriCount()
    {
        $criteria = new UserCriteria(__CLASS__);

        return self::doCount($criteria);
    }

    static public function getMaxAvailableBlocuri()
    {
        return sfConfig::get('mod_apartamente_max_blocuri') - self::getBlocuriCount();
    }

    static public function allDefined()
    {
        $criteria = new UserCriteria(__CLASS__);
        $blocuri = self::doSelect($criteria);

        $allDefined = true;
        if (count($blocuri))
        {
            foreach ($blocuri as $bloc)
            {
                if ($bloc->isIncomplete())
                {
                    $allDefined = false;
                    break;
                }
            }
        }
        else
        {
            $allDefined = false;
        }

        return $allDefined;
    }

}
